package com.fingard.dsp.bank.directbank.zbf01;

import java.util.HashMap;
import java.util.Map;

import com.fingard.dsp.bank.directbank.bankInterface.IOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.ReqOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.RetAtsOtherElse;
import com.fingard.text.MD5SaltHelper;
import com.fingard.text.StringHelper;

public class ZBF01OtherElse extends ZBF01Base implements IOtherElse {
	@Override
	public RetAtsOtherElse d_else(ReqOtherElse p_reqElse) throws Exception {
		String transType = p_reqElse.reqHeader.transType;
		if (transType.equals("VerifySign")) {
			return verifySign(p_reqElse);
		}
		if ("UnifiedSign".equals(transType)) {
			if ("POS".equals(p_reqElse.reqHeader.commandCode)) {
				return unifiedSignForPos(p_reqElse);
			}
		} else {
			RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
			retAts.retHashMap = new HashMap<String, String>();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", "未知的TransType：" + transType);
			return retAts;
		}
		return null;
	}

	/**
	 * POS保单信息查询签名
	 * 
	 * @param p_reqElse
	 * @return
	 */
	public RetAtsOtherElse unifiedSignForPos(ReqOtherElse p_reqElse) {
		RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
		retAts.retHashMap = new HashMap<String, String>();

		try {
			Map<String, String> mapParams = p_reqElse.reqHashMap;
			mapParams = removeHead(mapParams);

			String tmpSignData = getSignStr(mapParams);
			tmpSignData += "&key=" + getBankConfig().getValueAsString("saltKey");
			WriteBankLogLn2("待签名数据:\r\n" + tmpSignData);
			String sign = MD5SaltHelper.md5(tmpSignData, getCharset("UTF-8", "pos"));
			WriteBankLogLn2("签名结果:"+sign);
			if (StringHelper.hasAnyChar(sign)) {
				retAts.retHashMap.put("RespCode", "0");
				retAts.retHashMap.put("RespInfo", "");
				retAts.retHashMap.put("Result", sign);
			} else {
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "签名失败!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", ex.getMessage());
			WriteBankLogLn(ex);
		}

		return retAts;
	}
}
